#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019/7/26 下午5:33
# @Author  : fugang_le
# @Software: PyCharm

from ltp.pyltp_server import PyLtp
from nlp.similarty.CilinSimilarity.source.cilin import CilinSimilarity

'''
相似度评分标准
1.主语、宾语设为第1档 
2.状语设为第2档（时间、地点、原因、介宾等） 
3.谓语设为第3档 
4.主语、宾语的修饰关系设为第4档
'''

first_label = ['SBV', 'VOB', 'IOB', 'FOB']
second_label = ['ADV']
third_label = ['HED']
four_label = ['ATT', 'ADV', 'CMP', 'COO', 'POB', 'LAD', 'RAD', 'WP', 'IS', 'DBL']

first_weight = 4
second_weight = 3
third_weight = 2
four_weight = 1
score_dict = {'SBV': 4, 'VOB': 4, 'IOB': 4, 'FOB': 4,
              'ADV': 3,
              'HED': 2,
              'ATT': 1, 'ADV': 1, 'CMP': 1, 'COO': 1, 'POB': 1, 'LAD': 1, 'RAD': 1, 'WP': 1, 'IS': 1, 'DBL': 1}

candidate_list = ['流产假需要提供什么材料', '陪产假要交什么材料', '丧假需要提供哪些证明材料', '休病假要交什么材料',
                  '已转深户，我需要提交什么材料？', '如何休陪产假', '哪些休假是需要提交休假材料的？', '休假材料提交到哪？',
                  '陪产假有多少天', '如何提交材料']
sent = '''陪产假需要提供什么材料'''

ltp = PyLtp()
ltp.load_model_and_lexicon(['ALL'])
ltp_1 = ltp.process(sent)
ltp_11 = {arc.relation: ind for ind, arc in enumerate(ltp_1[0]['pars'])}
ltp_2 = [ltp.process(sent) for sent in candidate_list]
ltp.release_model()

cs = CilinSimilarity()
score_list = []
for pyload in ltp_2:
    score = 0
    for ind, arc in enumerate(pyload[0]['pars']):
        relation = arc.relation
        value = ltp_11.get(relation, None)
        if not value is None:
            word = ltp_1[0]['cws'][value]
        else:
            word = ''
        if word == '要':
            pass
        if pyload[0]['cws'][ind] == word or cs.similarity(pyload[0]['cws'][ind], word) == 1:
            score += score_dict[relation]
    score_list.append(score)

candidate_score = [[s, sent + '||' + candidate_list[index]] for index, s in enumerate(score_list)]
candidate_score.sort(key=lambda x: x[0], reverse=True)

for item in candidate_score:
    print(item)

# print('\t'.join("%d:%s:%d" % (ind + 1, arc.relation, arc.head) for ind, arc in enumerate(ltp_1[0]['pars'])))
# print(ltp_1)
